package com.java2007.dao.impl;

import com.java2007.dao.OrderDao;
import com.java2007.pojo.Order;
import com.java2007.util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 订单持久层
 * @author ZOU
 */
public class OrderDaoImpl implements OrderDao {
    /**
     * 订单持久层
     * @param order
     * @return
     */
    @Override
    public int insert(Order order) throws SQLException {
        QueryRunner qr=new QueryRunner();
        String sql="INSERT INTO t_order(id,table_id,total_price,pay_status,create_time,update_time,create_user)VALUES (?,?,?,?,?,?,?)";
        return qr.update(JdbcUtil.getConnection(),sql,order.getId(),order.getTableId(),order.getTotalPrice(),order.getPayStatus(),order.getCreateTime(),order.getUpdateTime(), order.getCreateUser());
    }

    /**
     * 查询同一桌的所有订单
     * @param tableId
     * @return
     */
    @Override
    public List<Order> findByTableId(Integer tableId, Integer status) throws SQLException {
        QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());
        String sql="select id Id,total_price totalPrice from t_order where table_id=? and pay_status=?";
        return qr.query(sql,new BeanListHandler<>(Order.class),tableId,status);
    }

    /**
     * 修改同一桌的所有订单的支付状态
     * @param tableId
     */
    @Override
    public int updateStatus(Integer tableId, int i) throws SQLException {
        QueryRunner qr=new QueryRunner();
        String sql="update t_order set pay_status=? where table_id=?";
        return qr.update(JdbcUtil.getConnection(),sql,i,tableId);
    }
    /**
     * 查询所有订单Id
     * @return
     */
    @Override
    public List<Order> findAllId(Integer tableId, int i) throws SQLException {
        QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());
        String sql="select id Id from t_order where table_id=? and pay_status=?";
        return qr.query(sql,new BeanListHandler<>(Order.class));
    }
}
